import { computed } from 'vue';

export default (props: any) => {
    const { $fn, $db, $global, $model } = props.ujt;
    function getAttrByStr(str: string, fl: string) {
        const tmpData: any = {};
        let tmpArr: any = str.split($model.data?.asai?.rulebook?.bksn);
        tmpData[fl] = tmpArr[0];
        tmpArr = tmpArr[1].split($model.data?.asai?.rulebook?.bkty);
        tmpData.sn = tmpArr[0];
        tmpArr = tmpArr[1].split($model.data?.asai?.rulebook?.bktm);
        tmpData.ty = tmpArr[0];
        tmpArr = tmpArr[1].split($model.data?.asai?.rulebook?.bktmg);
        try {
            tmpData.tm =
                tmpArr[0]?.length > 3
                    ? $fn.As.getTime(6, new Date(tmpArr[0]))
                    : '';
            tmpArr = tmpArr[1].split($model.data?.asai?.rulebook?.bkdesc);
            tmpData.tg =
                tmpArr[0]?.length > 3
                    ? $fn.As.getTime(6, new Date(tmpArr[0]))
                    : '';
        } catch (e) {}
        if (fl === 'li') {
            tmpArr = tmpArr[1].split($model.data?.asai?.rulebook?.bkcontent);
            tmpData.ds = tmpArr[0];
            tmpData.co = tmpArr[1];
        } else {
            tmpData.ds = tmpArr[1];
        }
        return tmpData;
    }
    const cmBookData: any = computed(() => {
        const tmpData: any = [];
        let tmpStr = $model?.[props.model]?.[props.book]?.replaceAll(
            '\r\n',
            '\n'
        );
        let chArr = tmpStr.split($model.data?.asai?.rulebook?.bkchannel);
        let chData: any, clData: any, liData: any;
        chArr.forEach((el: any) => {
            if (el) {
                let clArr = el.split($model.data?.asai?.rulebook?.bkclass);
                clArr.forEach((cl: any, clIndex: number) => {
                    if (clIndex == 0) {
                        chData = getAttrByStr(cl, 'ch');
                        chData.cc = [];
                        tmpData.push(chData);
                    } else {
                        let liArr = cl.split(
                            $model.data?.asai?.rulebook?.bklist
                        );
                        liArr.forEach((li: any, liIndex: number) => {
                            if (liIndex == 0) {
                                clData = getAttrByStr(li, 'cl');
                                clData.cc = [];
                                chData.cc.push(clData);
                            } else {
                                liData = getAttrByStr(li, 'li');
                                clData.cc.push(liData);
                            }
                        });
                    }
                });
            }
        });
        return tmpData;
    });

    return { cmBookData };
};
